package org.adit.spring.hibernate.infra;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class CacheResponseWrapper extends HttpServletResponseWrapper {
	 protected ServletOutputStream stream = null;
	    protected PrintWriter writer = null;
	    protected OutputStream cache = null;

	    public CacheResponseWrapper(HttpServletResponse paramHttpServletResponse, OutputStream paramOutputStream) {
	        super(paramHttpServletResponse);
	        this.cache = paramOutputStream;
	    }

	    public ServletOutputStream createOutputStream()
	            throws IOException {
	        return new CacheResponseOutputStream(this.cache);
	    }

	    public void flushBuffer()
	            throws IOException {
	        this.stream.flush();
	    }

	    public ServletOutputStream getOutputStream()
	            throws IOException {
	        if (this.writer != null)
	            throw new IllegalStateException("getWriter() has already been called!");
	        if (this.stream == null)
	            this.stream = createOutputStream();
	        return this.stream;
	    }

	    public PrintWriter getWriter()
	            throws IOException {
	        if (this.writer != null)
	            return this.writer;
	        if (this.stream != null)
	            throw new IllegalStateException("getOutputStream() has already been called!");
	        this.stream = createOutputStream();
	        this.writer = new PrintWriter(new OutputStreamWriter(this.stream, "ISO-8859-1"));
	        return this.writer;
	    }
}
